#include "csqltreemodel.h"
#include <QSqlQuery>
#include <QVector>
#include <QVariant>

#include "my_defs.h"

CSqlTreeModel::CSqlTreeModel(QObject *parent) :
    QSqlQueryModel(parent)
{
}

bool CSqlTreeModel::insertRows(int position, int rows, const QModelIndex &index/*=QModelIndex()*/)
{
    Q_UNUSED(index);
    beginInsertRows(QModelIndex(), position, position+rows-1);

    for (int row=0; row < rows; row++) {
        AlbumRecord t;
        m_allRecords.insert(position, t);
    }

    endInsertRows();
    return true;
}

bool CSqlTreeModel::removeRows(int position, int rows, const QModelIndex &index)
{
    Q_UNUSED(index);
    beginRemoveRows(QModelIndex(), position, position+rows-1);

    for (int row=0; row < rows; ++row)
    {
        m_allRecords.remove(position, rows);
    }

    endRemoveRows();
    return true;
}

int CSqlTreeModel::rowCount(const QModelIndex &parent) const
{
    return m_allRecords.count();
}

int CSqlTreeModel::columnCount(const QModelIndex &parent) const
{   // існує 4 колонки: uniqId, caption, styleId, comment
    return 4;
}

QVariant CSqlTreeModel::data(const QModelIndex &item, int role) const
{
    if (!item.isValid())
        return QVariant();

    if (item.row() >= rowCount())
         return QVariant();

    if (role == Qt::DisplayRole)
        return QVariant::fromValue( m_allRecords.at(item.row()) );

    return QVariant();
}

bool CSqlTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    // TODO: check conversation
    AlbumRecord t;
    // TODO: Check

    if (value.canConvert<AlbumRecord>())
        t = value.value<AlbumRecord>();

    if (index.isValid() && role == Qt::DisplayRole)
    {
        m_allRecords.replace(index.row(), t);
        emit dataChanged (index, index);
        return true;
    }
    return false;
}

QVariant CSqlTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal) {
        switch (section) {
            case 0: return "Стиль";
            case 1: return "Назва";
            case 2: return "Унікальний\nномер";
            case 3: return "Коментар";
            default: return QVariant();
        }
    }
    return QVariant();
}

Qt::ItemFlags CSqlTreeModel::flags(const QModelIndex &index) const
{
    if (!index.isValid())
        return Qt::ItemIsEnabled;
    return QAbstractTableModel::flags(index);
}

bool CSqlTreeModel::initParents(QSqlDatabase db)
{
    QSqlQuery query(GET_PARENTS_QUERY, db);
    query.exec(); // TODO:  query check

    while (query.next())
    {
        // value 0 -- styleId
        int styleId = query.value(0).toInt();   // TODO: check toInt();
        m_parents[styleId] = QModelIndex();
    }
    return true;
}

AlbumRecord::AlbumRecord()
{
    isParent = false;
    uniqId = 0;
    styleId = 0;
    caption = "";
    comment = "";
}

AlbumRecord::AlbumRecord(const AlbumRecord &a)
{
    isParent = a.isParent;
    uniqId = a.uniqId;
    styleId = a.styleId;
    caption = a.caption;
    comment = a.comment;

//    return this;
}

AlbumRecord::~AlbumRecord()
{
}

bool AlbumRecord::operator ==(const AlbumRecord &a)
{
    return isParent == a.isParent &&
            uniqId == a.uniqId &&
            styleId == a.styleId &&
            caption == a.caption &&
            comment == a.comment;
}

AlbumRecord& AlbumRecord::operator =(const AlbumRecord &a)
{
    isParent = a.isParent;
    uniqId = a.uniqId;
    styleId = a.styleId;
    caption = a.caption;
    comment = a.comment;

    return *this;
}

//bool AlbumRecord::operator <(const AlbumRecord &a) { return uniqId < a.uniqId; }

